package com.atguigu.srb.core.controller.admin;


import com.atguigu.common.exception.Assert;
import com.atguigu.common.exception.BusinessException;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author Dragon
 * @since 2021-07-29
 */
@Api(tags = "用户积分等级管理")
//@CrossOrigin
@RestController
@RequestMapping("/admin/core/integralGrade")
@Slf4j
public class AdminIntegralGradeController {

    @Resource
    private IntegralGradeService integralGradeService;

    @ApiOperation(value = "积分等级列表查询")
    @GetMapping("/list")
    public R listAll() {
        log.info("HELLO I AM SPRIDER");
        log.warn("WARNING!!!");
        log.error("THIS IS 404 ERROR");

        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list", list).message("获取消息成功");
    }

    @ApiOperation(value = "根据ID删除记录", notes = "逻辑删除")
    @DeleteMapping("/remove/{id}")
    public R removeById(
            @ApiParam(value = "输入的数字ID", example = "100", required = true)
            @PathVariable("id") Long id) {
        boolean Del = integralGradeService.removeById(id);
        if (Del) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }

    @ApiOperation(value = "新增积分等级")
    @PostMapping("/save")
    public R save(@ApiParam(value = "积分等级对象", required = true)
                  @RequestBody IntegralGrade integralGrade) {
        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        Assert.notZero(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NOT_ZERO);
        boolean save = integralGradeService.save(integralGrade);
        if (save) {
            return R.ok().message("新增积分成功");
        } else {
            return R.error().message("新增等级失败");
        }
    }


    @ApiOperation(value = "根据ID获取积分等级")
    @GetMapping("/get/{id}")
    public R getById(@ApiParam(value = "输入数字", example = "2", required = true)
                     @PathVariable("id") Long id) {
        IntegralGrade integralGrade = integralGradeService.getById(id);
        if (integralGrade != null) {
            return R.ok().data("integralGrade", integralGrade).message("获取消息成功");
        } else {
            return R.error().message("获取积分失败");
        }

    }

    @ApiOperation(value = "更新积分等级")
    @PutMapping("/update")
    public R updateById(@ApiParam(value = "输入ID",required = true)
                       @RequestBody IntegralGrade integralGrade){
        boolean b = integralGradeService.updateById(integralGrade);
      if (b){
          return  R.ok().message("更新成功");
      }
      return R.error().message("更新失败");
    }

}

